// const express = require('express');
import express from "express"
// const path=require("path")
import path from "path"
import cors from "cors"
// const userRoutes = require('./routes/users');
import userRoutes from './routes/users.js'
import uploadRoutes from './routes/upload.js'
import { __dirname,handlerSuccess } from "./utils/share.js"
import { ERROR_404 } from "./constant/index.js"
const app = express();
//从多个目录提供静态文件，但优先考虑“./public”而不是其他目录
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.static(path.join(__dirname, 'files')))
app.use(express.static(path.join(__dirname, 'uploads')))

// var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {flags: 'a'});
app.use(cors());
// app.use(morgan('short'));

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
//http://localhost:4444/users
app.use('/users', userRoutes);
app.use('/upload', uploadRoutes);
app.use((req, res, next) => {
  const {url,method,params,body} =req
  const resuls={...ERROR_404,data:`接口不存在：${method}请求,连接地址： ${url}`}
  res.status(404).json(resuls);
})

app.listen(4444, () => {
    console.log('Server running on http://localhost:4444');
});